package com.smartleanx.module.hrm.app.risk;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.enums.CommonStatusEnum;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.hrm.dal.dataobject.companysystem.CompanySystemDO;
import com.smartleanx.module.hrm.dal.dataobject.companysystem.CompanySystemDeptDO;
import com.smartleanx.module.hrm.dal.dataobject.companysystememployee.CompanySystemEmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.eums.employee.EmployeeStatusEnum;
import com.smartleanx.module.hrm.eums.risk.RiskLevelEnum;
import com.smartleanx.module.hrm.eums.risk.RiskTypeEnum;
import com.smartleanx.module.hrm.service.companysystem.CompanySystemDeptService;
import com.smartleanx.module.hrm.service.companysystem.CompanySystemService;
import com.smartleanx.module.hrm.service.companysystememployee.CompanySystemEmployeeService;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.risk.RiskService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 规章制度是否向员工告知
 *
 * @author tengcj
 * @since 2024/6/27
 */
@Component
public class RiskRegulationLeanHandleService implements RiskHandle {

    public static final long RISK_ID = RiskTypeEnum.REGULATION_NOT_INFORM.getCode();

    @Resource
    private RiskService riskService;

    @Resource
    private EmployeeService employeeService;

    @Resource
    private CompanySystemService companySystemService;

    @Resource
    private CompanySystemDeptService companySystemDeptService;

    @Resource
    private CompanySystemEmployeeService companySystemEmployeeService;

    @Override
    public Long type() {
        return RISK_ID;
    }

    @Override
    public void handleRisk(Set<Long> recordIdList) {
        List<CompanySystemDO> companySystemList = companySystemService.getCompanySystemList(recordIdList, null);
        if (CollUtil.isEmpty(companySystemList)) {
            riskService.deleteRisk(RISK_ID, recordIdList);
            return;
        }

        List<CompanySystemDeptDO> list = companySystemDeptService.getListByCompanySystemId(
                companySystemList.stream().map(CompanySystemDO::getId).collect(Collectors.toSet())
        );
        if (CollUtil.isEmpty(list)) {
            riskService.deleteRisk(RISK_ID, recordIdList);
            return;
        }

        List<EmployeeDO> employeeDOList = employeeService.getEmployeeListByDeptId(
                list.stream().map(CompanySystemDeptDO::getDeptId).collect(Collectors.toSet()), null
        );
        if (CollUtil.isEmpty(employeeDOList)) {
            riskService.deleteRisk(RISK_ID, recordIdList);
            return;
        }

        Map<Long, List<CompanySystemEmployeeDO>> employeeLearnedMap =
                CollectionUtils.convertMultiMap(
                        companySystemEmployeeService.getListByCompanySystemIds(
                                companySystemList.stream().map(CompanySystemDO::getId).collect(Collectors.toSet()
                                )
                        ), CompanySystemEmployeeDO::getCompanySystemId);

        for (CompanySystemDO companySystemDO : companySystemList) {
            List<CompanySystemEmployeeDO> companyLearnedList = employeeLearnedMap.get(companySystemDO.getId());
            Set<Long> empIdSet = CollectionUtils.convertSet(companyLearnedList, CompanySystemEmployeeDO::getEmployeeId);
            for (EmployeeDO employeeDO : employeeDOList) {
                if (EmployeeStatusEnum.SEPARATED.getCode().equals(employeeDO.getState())
                        || EmployeeStatusEnum.TO_BE_SEPARATED.getCode().equals(employeeDO.getState())) {
                    continue;
                }
                if (empIdSet.contains(employeeDO.getId())) {
                    riskService.deleteRisk(employeeDO.getId(), RISK_ID, companySystemDO.getId());
                } else {
                    riskService.addRisk(employeeDO.getId(), RISK_ID, RiskLevelEnum.MAJOR.getLevel(), companySystemDO.getId());
                }
            }
        }
    }
}
